<?php


namespace app\manage\controller;


use app\common\model\CouponGenerate;
use app\common\model\MemberQy;
use app\common\model\Coupon as CouponModel;
class DataStatistics extends Base
{
    public function index(){
        // 当前页数
        $page   = input('get.page/d', 1);
        // 查询条件
        $where  = $this->where();
        // 每页条数
        $limit  = $this->limit();
        // 分页条件
        $offset = $this->offset($page, $limit);
        // 分页数据
        $data = CouponGenerate::where($where)->append(['staff_name','staff_used_numbers'])
            ->field('id,coupon_id,staff_id,release_time')->limit($offset)->group('staff_id')->select();

        $count  = CouponGenerate::where($where)->group('staff_id')->count('id');
//         列表附加
//        $this->indexAttach($data);

        return $this->fetch('', [
            'page'  => $page,
            'count' => $count,
            'limit' => $limit,
            'data'  => $data,
        ]);
    }
    /**
     * 每页条数
     */
    public function limit()
    {
        return config('site.default_admin_limit');
    }
    /**
     * 组合 where 查询条件
     */
    public function where()
    {
        $keywords = input('keywords');
        $riTime = input('riTime');
        $yueTime = input('yueTime');
        $staff_id = [];
        if ($keywords){
            $staff = MemberQy::where("name","like","%".$keywords."%")->field('id')->select();
            foreach ($staff as $key => $value){
                $staff_id[] = $value['id'];
            }
            if (empty($staff_id)){
                $where[] = [
                    ['staff_id',">=",0]
                ];
            }else{
                $where[] = [
                    ['staff_id',"in",$staff_id]
                ];
            }
        }else{
            $where[] = [
                ['staff_id',">=",0]
            ];
        }
        if ($riTime) {
            $day = $riTime;
            $where[] = [
                ['reviewed_time', '>=', strtotime($riTime)],
                ['reviewed_time', '<=', strtotime($riTime) + (60*60*24-1)],
            ];
        }else{
            $day =  date('Y-m-d',time());
            $where[] = [
                ['reviewed_time', '>=', strtotime($day)],
                ['reviewed_time', '<=', strtotime($day) + (60*60*24-1)],
            ];
        }
        if ($yueTime) {
            $day = $yueTime;
            $days   = date('d', strtotime("{$yueTime} + 1 month -1 day"));
            $where[] = [
                ['reviewed_time', '>=', strtotime($yueTime)],
                ['reviewed_time', '<=', strtotime($yueTime) + ((60*60*24) * $days)],
            ];
        }
        $where[] = [
            ['is_release','eq',1],
        ];

        $header = CouponModel::withTrashed()->field("id,coupon_name")->order('id')->select();

        $this->assign('header',$header);
        $this->assign('keywords',$keywords);
        $this->assign('riTime', $riTime);
        $this->assign('yueTime', $yueTime);
        $this->assign('day', $day);
        return $where;
    }
    public function verification_log(){
        // 当前页数
        $page   = input('get.page/d', 1);
        // 员工
        $staff_id = input('staff_id');
        //优惠券
        $coupon_id = input('coupon_id');
        // 查询条件
        $where[]  = [
            ["staff_id","eq",$staff_id],
            ["coupon_id","eq",$coupon_id],
            ["status","eq",1]
        ];
        // 每页条数
        $limit  = $this->limit();
        // 分页条件
        $offset = $this->offset($page, $limit);
        // 分页数据

        $data =   CouponGenerate::where($where)->limit($offset)->field('id')->append(['verification_info'])->order("reviewed_time desc")->select();

        $count  = CouponGenerate::where($where)->count('id');
//         列表附加
//        $this->indexAttach($data);

        return view('/log', [
            'page'  => $page,
            'count' => $count,
            'limit' => $limit,
            'data'  => $data,
        ]);
    }
}